//
//  ImageViewController.m
//  PhotoGirl
//
//  Created by comic on 4/20/13.
//  Copyright (c) 2013 comic. All rights reserved.
//

#import "ImageViewController.h"
#import "TileImageView.h"
#import "IoObjectRequest.h"
#import "AppDelegate.h"
@interface ImageViewController ()

@end

@implementation ImageViewController
@synthesize photo=_photo;
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    [self.tileContainerView initTapDetect];
    [self.view addSubview:self.tileContainerView];
    [self.tileContainerView setDataSource:self];
    [self.tileContainerView.tileContainerView setDelegate:self];
    [self.tileContainerView setTileSize:CGSizeMake(self.photo.tileX, self.photo.tileY)];
    
    [self.tileContainerView setBouncesZoom:YES];
    [self.tileContainerView setMaximumResolution:self.photo.getMaxResolution];
    [self.tileContainerView setMinimumResolution:1];
    [self.tileContainerView setMaximumZoomScale:self.photo.getMaxResolution+1];
    [self.tileContainerView setMinimumZoomScale:1];
    [self.tileContainerView setPhoto:self.photo];

    [self.tileContainerView reloadDataWithNewContentSize:self.tileContainerView.frame.size inResolution:2 withZoomScale:0.2];
    self.toolBar.alpha=0.5;
    [self.view bringSubviewToFront:self.toolBar];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated
{
     self.navigationController.navigationBar.barStyle=UIBarStyleBlackOpaque;
}
- (void)viewWillAppear:(BOOL)animated {
     self.navigationController.navigationBar.barStyle=UIBarStyleBlackTranslucent;
}
- (void)viewDidUnload {
   
    [self setToolBar:nil];
    [self setImageBarControl:nil];
    [self setTileContainerView:nil];
    [super viewDidUnload];
}

#pragma mark Tile Datasource
- (TileImageView *)tiledScrollView:(TiledScrollView *)scrollView tileForRow:(int)row column:(int)column resolution:(int)resolution {
    TileImageView *tileImage=[[TileImageView alloc] initWithFrame:CGRectZero];
   
    int tilePosition=row*([self.photo getMaxColWithResolution:resolution]+1) + column;
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@&JTL=%d,%d", kPhotoBaseURL,self.photo.imageName,resolution,tilePosition]];
    IoObjectRequest *request=[[IoObjectRequest alloc] initWithURL:url];
    [request setTimeOutSeconds:10];
    [request setInterfaceObject:tileImage];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestDone:)];
    [request setDidFailSelector:@selector(requestWentWrong:)];
    [[[AppDelegate sharedAppDelegate] downloadQueue] addOperation:request];
    tileImage.row=row;
    tileImage.col=column;
    tileImage.resolution=resolution;
    tileImage.width=[self.photo getTileSize:tileImage].width;
    tileImage.height=[self.photo getTileSize:tileImage].height;
    return tileImage;
}

- (void)requestDone:(IoObjectRequest *)request
{
    if ([request.interfaceObject isKindOfClass:[TileImageView class]]) {
        UIImage *image=[UIImage imageWithData:request.rawResponseData];
        TileImageView *tileImage=request.interfaceObject;
        tileImage.image=image;
    }
}

- (void)requestWentWrong:(IoObjectRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@-code:%d",error.localizedDescription,error.code);
}
#pragma mark Tap Delegate
- (void)tapDetectingView:(TapDetectingView *)view gotDoubleTapAtPoint:(CGPoint)tapPoint {
    // double tap zooms in
    float newScale = [self.tileContainerView zoomScale] * 1.5;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
    [self.tileContainerView zoomToRect:zoomRect animated:YES];
}

- (void)tapDetectingView:(TapDetectingView *)view gotTwoFingerTapAtPoint:(CGPoint)tapPoint {
    // two-finger tap zooms out
    float newScale = [self.tileContainerView zoomScale] / 1.5;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
    [self.tileContainerView zoomToRect:zoomRect animated:YES];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
    
    CGRect zoomRect;
    
    // the zoom rect is in the content view's coordinates.
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [self.tileContainerView frame].size.height / scale;
    zoomRect.size.width  = [self.tileContainerView frame].size.width  / scale;
    
    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    
    return zoomRect;
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight || toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        self.imageBarControl.width=screenRect.size.height-160;
    } else {
        self.imageBarControl.width=screenRect.size.width-160;
    }
}
@end
